package com.free.mysql.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.free.freemarker.constant.Constant;
import com.free.freemarker.constant.SqlConstant;
import com.free.mysql.MyConstant;
import com.mysql.cj.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;


public class InitConfigure {

    private String propertiesName = "codeBuild.properties";

    private String root;

    public static void build() throws Exception {
        new InitConfigure().init();
    }


    private void init() throws Exception {
        root = System.getProperty("user.dir");
        root = root.substring(0, root.lastIndexOf(File.separator));
        System.out.println(root);

//        root = "F:\\project\\codeBuilder\\target\\codeBuilder";
        Properties pro = new Properties();
        FileInputStream in = new FileInputStream(root + File.separator + propertiesName);
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        pro.load(bf);
        read(pro);
        in.close();
    }

    private void read(Properties pro) {
        SqlConstant.SQL_URL = pro.getProperty("url");
        SqlConstant.USER = pro.getProperty("user");
        SqlConstant.PASSWORD = pro.getProperty("password");
        SqlConstant.SQL_DRIVER = pro.getProperty("sql.driver");

        Constant.basePath = root;

        Constant.ANNOTATION_AUTHOR_NAME = pro.getProperty("author.name");
        Constant.ANNOTATION_AUTHOR_MAIL = pro.getProperty("author.mail");
        Constant.ANNOTATION_VERSION = pro.getProperty("version");

        Constant.ftlPath = root + File.separator + pro.getProperty("ftl.path");
        Constant.filePath = root + File.separator + pro.getProperty("file.path");

        Constant.TABLE_PREFIX = pro.getProperty("table.prefix");

        String VIEW_FTL = pro.getProperty("VIEW_FTL");
        if (VIEW_FTL != null && !"".equalsIgnoreCase(VIEW_FTL)) {
            Constant.VIEW_FTL = JSONArray.parseArray(VIEW_FTL);
        }

        String JAVA_FTL = pro.getProperty("JAVA_FTL");
        if (JAVA_FTL != null && !"".equals(JAVA_FTL)) {
            Constant.JAVA_FTL = JSONArray.parseArray(JAVA_FTL);
        }

        String Mybatis_FTL = pro.getProperty("Mybatis_FTL");
        if (Mybatis_FTL != null && !"".equals(Mybatis_FTL)) {
            Constant.Mybatis_FTL = JSONObject.parseObject(Mybatis_FTL);
        }

        MyConstant.PACKAGE_BASE_URL = pro.getProperty("package.base");
        MyConstant.ENTITY_BASE_URL = pro.getProperty("package.entity");
        MyConstant.DAO_BASE_URL = pro.getProperty("package.dao");
        MyConstant.SERVICE_BASE_URL = pro.getProperty("package.i.service");
        MyConstant.SERVICE_IMPL_BASE_URL = pro.getProperty("package.service");
        MyConstant.CONTROLLER_BASE_URL = pro.getProperty("package.controller");
        MyConstant.TABLE_MESSAGE = pro.getProperty("tables");


        String suffix = pro.getProperty("dl.cs");
        if (!StringUtils.isNullOrEmpty(suffix)) {
            MyConstant.DAO_IMPL_SUFFIX = suffix;
        } else {
            MyConstant.DAO_IMPL_SUFFIX = "";
        }

        suffix = pro.getProperty("d.cs");
        if (!StringUtils.isNullOrEmpty(suffix)) {
            MyConstant.DAO_SUFFIX = suffix;
        } else {
            MyConstant.DAO_SUFFIX = "";
        }

        String prefix = pro.getProperty("s.cp");
        if (!StringUtils.isNullOrEmpty(prefix)) {
            MyConstant.SERVICE_PREFIX = prefix;
        } else {
            MyConstant.SERVICE_PREFIX = "";
        }

        suffix = pro.getProperty("s.cs");
        if (!StringUtils.isNullOrEmpty(suffix)) {
            MyConstant.SERVICE_SUFFIX = suffix;
        } else {
            MyConstant.SERVICE_SUFFIX = "";
        }

        suffix = pro.getProperty("sl.cs");
        if (!StringUtils.isNullOrEmpty(suffix)) {
            MyConstant.SERVICE_IMPL_SUFFIX = suffix;
        } else {
            MyConstant.SERVICE_IMPL_SUFFIX = "";
        }
    }
}
